﻿(function(exports) {
	$(document).ready(function() {

		/**
		 * Define display module
		 *
		 */

		var AvifaunaDisplay = (function() {
			
			function init() {
				alignDOMElements();
				showUI();
			}

			function alignDOMElements() {
				$("#mainBirdPanel").css("top", ($("body").height() - $("#mainBirdPanel").height()) / 2);
				$("#mainBirdPanel").css("left", ($("body").width() - $("#mainBirdPanel").width()) / 2);
				$("#mainMenu").css("top", ($("body").height() - $("#mainMenu").height()) / 2);
				$("#mainMenu").css("left", ($("body").width() - $("#mainMenu").width()) / 2);
			};

			function alignRiddleAnswers() {
				if($("#availableRiddleAnswers ul li").length < 5) {
					$("#availableRiddleAnswers ul li").css("margin-left", (($("#availableRiddleAnswers").width() - $("#availableRiddleAnswers ul li").length * ($("#availableRiddleAnswers ul li").width() + 2)) / ($("#availableRiddleAnswers ul li").length - 1) / 4) + "px");
					$("#availableRiddleAnswers ul li").css("margin-right", (($("#availableRiddleAnswers").width() - $("#availableRiddleAnswers ul li").length * ($("#availableRiddleAnswers ul li").width() + 2)) / ($("#availableRiddleAnswers ul li").length - 1) / 4) + "px");
				}
			};

			function hideTutorial() {
				$("#tutorial").fadeOut();
			}

			function hideVolumeControl() {
				$("#volumeInput").hide();
			};

			function onBackToMainMenu() {
				$("#mainMenu").fadeIn();
				$("#mainBirdPanel").fadeOut();
				$("#backButton").fadeOut();
				$("#currentScore").fadeOut();
				$("#tutorial").fadeOut();

				$("#copyrightInfo .riddle").hide();
				$("#copyrightInfo .available").hide();
				$("#copyrightInfo .application").show();
			};

			function onBeginTraining() {
				$("#previousBirdButton").hide();

				$("#mainMenu").fadeOut();
				$("#mainBirdPanel").fadeIn();
				$("#backButton").fadeIn();
			};

			function onFinishTraining(training) {
				$("#copyrightInfo .riddle").hide();
				$("#copyrightInfo .available").show();
				$("#copyrightInfo .content .image-author").html("Evanherk");
				$("#copyrightInfo .content .image-license").html(parseLicenseField("cc-by-sa-3.0"));
				$("#copyrightInfo .content .recording-author").html("");
				$("#copyrightInfo .content .recording-license").html("");
				$("#copyrightInfo .content .description-author").html("");
				$("#copyrightInfo .content .description-license").html("");

				$("#mainBirdPanel h2").text("Brawo!");
				$("#mainBirdImage").show();
				$("#mainBirdImage").attr("src", "data/images/happy-athene-noctua.jpg");

				$("#mainBirdDescription").show().text(training.trainingSet.successMessage);

				$("#availableRiddleAnswers").hide();

				$("#mainBirdPanelVUMeter").hide();

				$("#tutorial").hide();
			};

			function onMainBirdImageLoad(event) {
				mainBirdImage = $("#mainBirdImage");
				mainBirdImage.css("top", (330 - mainBirdImage.height()) / 2);
				availableHorizontalSpace = $("#mainBirdPanel").width() - $("#mainBirdImage").width() - 45;
				$("#mainBirdDescription").width(availableHorizontalSpace);
			};

			function onRiddleAnswerImageLoad(img) {
				maxHeight = parseInt($(img).css("max-height"));
				diff = maxHeight - $(img).height();
				if(diff > 0) {
					$(img).css("margin-top", (diff / 2) + "px");
				}
			};

			function onSubmitRiddleSolution(riddleAnswer, rightAnswer) {
				$(".riddleAnswer").addClass("inactive");
				$(riddleAnswer).addClass("wrongAnswer");
				$(".riddleAnswer img[data-avifauna-bird=" + rightAnswer.id + "]").parent().addClass("rightAnswer");
				$("#mainBirdPanelNavigation").show();
			}

			function onUpdateScore(guessed, total) {
				$("#currentScore .bigLabel span").text(guessed + "/" + total);
				$("#currentScore").show();
			};

			function parseAuthorField(text) {
				var parsedText = text;
				if(text.match(/^http[s]?:\/\/.*wikipedia\.org.*/)) {
					parsedText = "Autorzy aplikacji, na podstawie <a href='" + text + "' target='_blank'>Wikipedii</a>"
				}
				return parsedText;
			};

			function parseLicenseField(text) {
				var parsedText = text;
				if(text === "pd") {
					parsedText = "<a href='https://pl.wikipedia.org/wiki/Domena_publiczna' target='_blank'>Domena publiczna</a>";
				} else if(text.match(/cc-(..-)+\d\.\d/)) {
					restrictions = text.match(/(-\w\w)+/)[0].substring(1);
					version = text.match(/\d\.\d/)[0];
					parsedText = "<a href='http://creativecommons.org/licenses/" + restrictions + "/" + version + "/' target='_blank'>CC " + restrictions.toUpperCase() + " " + version + "</a>";
				}
				return parsedText;
			};

			function showBird(bird) {
				$("#mainBirdPanel div.panelContentWrapper").fadeOut("fast", function() {
					$("#mainBirdImage").show();
					$("#mainBirdImage").attr("src", bird.getImagePath());

					updateMainBirdPanelHeader(bird.getLocalizedName(AvifaunaConfig.locale));
					$("#mainBirdPanel h3").show().text(bird.getLatinName());
					$("#mainBirdDescription").show().text(bird.getLocalizedDescription(AvifaunaConfig.locale));

					$("#mainBirdPanelNavigation").show();
					$("#availableRiddleAnswers").hide();

					$("#mainBirdPanelVUMeter").width(AvifaunaConfig.vuMeterDefaultWidth);
					$("#mainBirdPanelVUMeter").removeClass("riddlePlacement");

					$("#mainBirdPanel div.panelContentWrapper").fadeIn("fast", function() {
						AvifaunaSound.playSound(bird.getSoundPath());
						updateCopyrightInfo(bird);
					});
				});
			}

			function showBirdRiddle(riddleAnswers, rightAnswer) {
				$("#mainBirdPanel div.panelContentWrapper").fadeOut("fast", function() {

					$("#mainBirdImage").hide();

					updateMainBirdPanelHeader("Który to ptak?");
					$("#mainBirdPanel h3").hide();
					$("#mainBirdDescription").hide();

					$("#mainBirdPanelNavigation").hide();
					$("#availableRiddleAnswers").show();

					$("#mainBirdPanelVUMeter").width(AvifaunaConfig.vuMeterExpandedWidth);
					$("#mainBirdPanelVUMeter").addClass("riddlePlacement");
					riddleAnswersListMarkup = "";

					riddleAnswers.forEach(function(currentBird) {
						liMarkup = "<li class='riddleAnswer'><img src='" + currentBird.getImagePath() + "' class='riddleAnswerImage' data-avifauna-bird='" + currentBird.id + "'/>" + "<span class='riddleAnswerCaption'>" + currentBird.getLocalizedName(AvifaunaConfig.locale) + "</span></li>"
						riddleAnswersListMarkup += liMarkup;
					});

					$("#availableRiddleAnswers ul").html(riddleAnswersListMarkup);
					$("#availableRiddleAnswers ul .riddleAnswerImage").load(function() {
						onRiddleAnswerImageLoad(this);
					});
					alignRiddleAnswers();

					$("#mainBirdPanel div.panelContentWrapper").fadeIn("fast", function() {
						$(".riddleAnswer").click(function(event) {
							AvifaunaCore.submitRiddleSolution(event.currentTarget, rightAnswer);
						});
						AvifaunaSound.playSound(bird.getSoundPath());
						updateCopyrightInfo();
					});

					$("#tutorial span").text("Wskaż ptaka, którego odgłos jest odtwarzany.")
				});
			};
			
			function showPreviousBirdButton() {
				$("#previousBirdButton").show();
			};

			function showUI() {
				$("#mainMenu").fadeIn();
				$("#tutorial").css("left", ($("body").width() - $("#tutorial").width()) / 2);
				$("#tutorial").fadeIn();
			};

			function showVolumeControl() {
				$("#volumeInput").show();
			};

			function showVUMeter() {
				$("#mainBirdPanelVUMeter").show();
			}

			function updateCopyrightInfo(bird) {

				if(bird) {
					imageAuthor = parseAuthorField(bird.data.imageAuthor);
					imageLicense = parseLicenseField(bird.data.imageLicense);
					recordingAuthor = parseAuthorField(bird.data.soundAuthor);
					recordingLicense = parseLicenseField(bird.data.soundLicense);
					descriptionAuthor = parseAuthorField(bird.getLocalizedDescritpionAuthor(AvifaunaConfig.locale));
					descriptionLicense = parseLicenseField(bird.getLocalizedDescritpionLicense(AvifaunaConfig.locale));
					$("#copyrightInfo .riddle").hide();
					$("#copyrightInfo .available").show();
					$("#copyrightInfo .application").hide();
				} else {
					imageAuthor = imageLicense = recordingAuthor = recordingLicense = descriptionAuthor = descriptionLicense = "Obecnie niedostępne";
					$("#copyrightInfo .riddle").show();
					$("#copyrightInfo .available").hide();
					$("#copyrightInfo .application").hide();
				}

				$("#copyrightInfo .content .image-author").html(imageAuthor);
				$("#copyrightInfo .content .image-license").html(imageLicense);
				$("#copyrightInfo .content .recording-author").html(recordingAuthor);
				$("#copyrightInfo .content .recording-license").html(recordingLicense);
				$("#copyrightInfo .content .description-author").html(descriptionAuthor);
				$("#copyrightInfo .content .description-license").html(descriptionLicense);

			};

			function updateMainBirdPanelHeader(text) {
				$("#mainBirdPanel h2").text(text);
			};

			function updateTutorial(text) {
				$("#tutorial span").text(text);
			};

			function updateVUMeter(value) {
				$("#mainBirdPanelVUMeter span").css("width", Math.round(value * 100) + "%");
			};

			return {
				init: init,
				alignDOMElements: alignDOMElements,
				hideTutorial : hideTutorial,
				hideVolumeControl : hideVolumeControl,
				onBackToMainMenu : onBackToMainMenu,
				onBeginTraining : onBeginTraining,
				onFinishTraining : onFinishTraining,
				onMainBirdImageLoad : onMainBirdImageLoad,
				onSubmitRiddleSolution : onSubmitRiddleSolution,
				onUpdateScore : onUpdateScore,
				showBird : showBird,
				showBirdRiddle : showBirdRiddle,
				showPreviousBirdButton : showPreviousBirdButton,
				showVolumeControl : showVolumeControl,
				showVUMeter : showVUMeter,
				updateCopyrightInfo : updateCopyrightInfo,
				updateMainBirdPanelHeader : updateMainBirdPanelHeader,
				updateTutorial : updateTutorial,
				updateVUMeter : updateVUMeter
			};
		})();

		exports.AvifaunaDisplay = AvifaunaDisplay;

	});
})(window);
